package cn.edu.scau.cmi.huangmaoqin.client;

import cn.edu.scau.cmi.huangmaoqin.Singleton.Chairman;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class SingletonClients extends Application {
	@Override
	public void start(Stage primaryStage) {
		primaryStage.setTitle("Singleton");
		primaryStage.setResizable(false);
		Label label = new Label("单例模式\n\n创建主席名字：");
		label.setTranslateX(20);
		label.setTranslateY(10);
		TextField text = new TextField();
		text.setTranslateY(25);
		text.setTranslateX(20);
		Button ok = new Button("确定");
		ok.setTranslateY(25);
		ok.setTranslateX(25);
		TextArea show = new TextArea();
		show.setTranslateX(20);
		show.setTranslateY(25);
		show.setMinHeight(250);
		show.setMinWidth(300);
		ok.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Chairman chairman = Chairman.getInstance(text.getText());
				String warn = "该模式下已有对象被创建，无法创建对象了！你现在获得的对象是：\n";
				if (show.getText().length() != 0)
					show.appendText(warn + chairman.getName() + chairman + "\n");
				else
					show.setText(chairman.getName() + chairman + "\n");
			}

		});
		FlowPane root = new FlowPane();
		root.getChildren().add(label);
		root.getChildren().add(text);
		root.getChildren().add(ok);
		root.getChildren().add(show);
		primaryStage.setScene(new Scene(root, 550, 350));
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
